<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Elasticsearch2.3.4分词插件开发 - (power up)</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="Kevin Jiang" /><meta name="description" content="0. Elasticsearch的版本是2.3.4 首先你得有自己的一Analyzer和Tokenizer，可以参考前面的一篇文章 1. pom.xml 使用mave" /><meta name="keywords" content="KevinJiang, AI大模型, AI落地, 全栈工程师, Java, Spring Boot" />






<meta name="generator" content="Hugo 0.84.4 with theme even" />


<link rel="canonical" href="http://kevinjiang.info/post/searchengine/elasticsearch2.3.4%E5%88%86%E8%AF%8D%E6%8F%92%E4%BB%B6%E5%BC%80%E5%8F%91/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.78f8f17bab244b9ee62ad16480c9584d5fc2db06ae20681d1ca225cefd80767c.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="Elasticsearch2.3.4分词插件开发" />
<meta property="og:description" content="0. Elasticsearch的版本是2.3.4 首先你得有自己的一Analyzer和Tokenizer，可以参考前面的一篇文章 1. pom.xml 使用mave" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://kevinjiang.info/post/searchengine/elasticsearch2.3.4%E5%88%86%E8%AF%8D%E6%8F%92%E4%BB%B6%E5%BC%80%E5%8F%91/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2016-12-16T15:42:54+00:00" />
<meta property="article:modified_time" content="2016-12-16T15:42:54+00:00" />

<meta itemprop="name" content="Elasticsearch2.3.4分词插件开发">
<meta itemprop="description" content="0. Elasticsearch的版本是2.3.4 首先你得有自己的一Analyzer和Tokenizer，可以参考前面的一篇文章 1. pom.xml 使用mave"><meta itemprop="datePublished" content="2016-12-16T15:42:54+00:00" />
<meta itemprop="dateModified" content="2016-12-16T15:42:54+00:00" />
<meta itemprop="wordCount" content="2161">
<meta itemprop="keywords" content="Elasticsearch,分词," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Elasticsearch2.3.4分词插件开发"/>
<meta name="twitter:description" content="0. Elasticsearch的版本是2.3.4 首先你得有自己的一Analyzer和Tokenizer，可以参考前面的一篇文章 1. pom.xml 使用mave"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">(Power up)</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/resume/">
        <li class="mobile-menu-item">简历</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">(Power up)</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/resume/">简历</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">Elasticsearch2.3.4分词插件开发</h1>

      <div class="post-meta">
        <span class="post-time"> 2016-12-16 </span>
        
          <span class="more-meta"> 约 2161 字 </span>
          <span class="more-meta"> 预计阅读 5 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li><a href="#0-elasticsearch的版本是234">0. Elasticsearch的版本是2.3.4</a></li>
    <li><a href="#1-pomxml">1. pom.xml</a></li>
    <li><a href="#2-继承plugin">2. 继承Plugin</a></li>
    <li><a href="#2-实现analysisbinderprocessor">2. 实现AnalysisBinderProcessor</a></li>
    <li><a href="#3-实现abstracttokenizerfactory">3. 实现AbstractTokenizerFactory</a></li>
    <li><a href="#4-实现abstractindexanalyzerprovider">4. 实现AbstractIndexAnalyzerProvider</a></li>
    <li><a href="#5-package">5. package</a></li>
    <li><a href="#6-打包安装">6. 打包安装</a></li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h2 id="0-elasticsearch的版本是234">0. Elasticsearch的版本是2.3.4</h2>
<p>首先你得有自己的一<code>Analyzer</code>和<code>Tokenizer</code>，可以参考前面的<a href="/2016/12/16/%E8%87%AA%E5%AE%9A%E4%B9%89Lucene%E7%9A%84%E5%88%86%E8%AF%8D%E5%99%A8Analyzer">一篇文章</a></p>
<h2 id="1-pomxml">1. pom.xml</h2>
<p>使用maven来构建项目，需要包含elasticsearch的依赖，否则编译不了</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;dependency&gt;</span>
    <span class="nt">&lt;groupId&gt;</span>org.elasticsearch<span class="nt">&lt;/groupId&gt;</span>
    <span class="nt">&lt;artifactId&gt;</span>elasticsearch<span class="nt">&lt;/artifactId&gt;</span>
    <span class="nt">&lt;version&gt;</span>2.3.4<span class="nt">&lt;/version&gt;</span>
    <span class="nt">&lt;scope&gt;</span>provided<span class="nt">&lt;/scope&gt;</span>
<span class="nt">&lt;/dependency&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>然后es的plugin需要一个描述文件<code>plugin-descriptor.properties</code>，把这个文件放在<code>src/main/resources</code>这个目录，打包后安装到es里面就会出现在插件目录下了。这个文件需要写入下面的内容：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">description=${project.description}
version=${project.version}
name=${elasticsearch.plugin.name}
site=${elasticsearch.plugin.site}
jvm=${elasticsearch.plugin.jvm}
classname=${elasticsearch.plugin.classname}
java.version=${elasticsearch.plugin.java.version}
elasticsearch.version=${elasticsearch.version}
</code></pre></td></tr></table>
</div>
</div><p>这些才使用了maven在<code>pom.xml</code>定义的内容，直接定义在<code>pom.xml</code>的根路径下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;properties&gt;</span>
    <span class="nt">&lt;project.build.sourceEncoding&gt;</span>UTF-8<span class="nt">&lt;/project.build.sourceEncoding&gt;</span>
    <span class="nt">&lt;elasticsearch.plugin.name&gt;</span>elasticsearch-analysis-demo<span class="nt">&lt;/elasticsearch.plugin.name&gt;</span>
    <span class="nt">&lt;elasticsearch.plugin.site&gt;</span>false<span class="nt">&lt;/elasticsearch.plugin.site&gt;</span>
    <span class="nt">&lt;elasticsearch.plugin.jvm&gt;</span>true<span class="nt">&lt;/elasticsearch.plugin.jvm&gt;</span>
    <span class="nt">&lt;elasticsearch.plugin.java.version&gt;</span>1.8<span class="nt">&lt;/elasticsearch.plugin.java.version&gt;</span>
    <span class="nt">&lt;elasticsearch.version&gt;</span>2.3.4<span class="nt">&lt;/elasticsearch.version&gt;</span>
    <span class="nt">&lt;elasticsearch.plugin.classname&gt;</span>org.elasticsearch.plugin.AnalysisDemoPlugin<span class="nt">&lt;/elasticsearch.plugin.classname&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>最重要的就是<code>plugin-descriptor.properties</code>里面的<code>classname</code>，也就是<code>pom.xml</code>定义的<code>elasticsearch.plugin.classname</code>节点里面的类名，这个类就是es加载plugin的入口。</p>
<h2 id="2-继承plugin">2. 继承Plugin</h2>
<p>现在来看Java代码，分词插件入口类需要继承<code>org.elasticsearch.plugins.Plugin</code>类，并实现三个方法：</p>
<ol>
<li>public String name()  返回插件名字</li>
<li>public String description  返回插件的描述</li>
<li>public void onModule(AnalysisModule model)  可以使用<code>model</code>这个<code>AnalysisModule</code>对象添加自定义的分词器</li>
</ol>
<p>上代码：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">org.elasticsearch.plugin</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.lexiscn.elasticsearch.hylanda.HylandaAnalysisBinderProcessor</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.index.analysis.AnalysisModule</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.plugins.Plugin</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">AnalysisDemoPlugin</span> <span class="kd">extends</span> <span class="n">Plugin</span> <span class="o">{</span>

	<span class="kd">public</span> <span class="kd">static</span> <span class="n">String</span> <span class="n">PLUGIN_NAME</span> <span class="o">=</span> <span class="s">&#34;elasticsearch-analysis-demo&#34;</span><span class="o">;</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="n">String</span> <span class="nf">name</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="n">PLUGIN_NAME</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="n">String</span> <span class="nf">description</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="n">PLUGIN_NAME</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onModule</span><span class="o">(</span><span class="n">AnalysisModule</span> <span class="n">model</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">model</span><span class="o">.</span><span class="na">addProcessor</span><span class="o">(</span><span class="k">new</span> <span class="n">HylandaAnalysisBinderProcessor</span><span class="o">());</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p>在<code>model.addProcessor</code>这个方法调用的时候，需要一个<code>org.elasticsearch.index.analysis.AnalysisModule.AnalysisBinderProcessor</code>对象。所以还需要实现一个继承处<code>AnalysisBinderProcessor</code>的类：</p>
<h2 id="2-实现analysisbinderprocessor">2. 实现AnalysisBinderProcessor</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">org.elasticsearch.demo</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">org.elasticsearch.index.analysis.AnalysisModule</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">DemoAnalysisBinderProcessor</span> <span class="kd">extends</span> <span class="n">AnalysisModule</span><span class="o">.</span><span class="na">AnalysisBinderProcessor</span> <span class="o">{</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">processAnalyzers</span><span class="o">(</span><span class="n">AnalyzersBindings</span> <span class="n">analyzersBindings</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">analyzersBindings</span><span class="o">.</span><span class="na">processAnalyzer</span><span class="o">(</span><span class="s">&#34;hylanda_analyzer&#34;</span><span class="o">,</span> <span class="n">DemoAnalyzerProvider</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">processTokenizers</span><span class="o">(</span><span class="n">TokenizersBindings</span> <span class="n">tokenizersBindings</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">tokenizersBindings</span><span class="o">.</span><span class="na">processTokenizer</span><span class="o">(</span><span class="s">&#34;hylanda_tokenizer&#34;</span><span class="o">,</span> <span class="n">DemoTokenizerFactory</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p>这个继承自<code>AnalysisBinderprocessor</code>的类根据需要实现<code>processAnalyzers</code>和<code>processTokenizers</code>方法。一个添加analyzer，一个是添加tokenizer，第一个参数就是analyzer或者tokenizer的名字。这里就可以添加自己的分词器了，不过还需要一点点努力。如果是Analyzer还需要一个继承自<code>org.elasticsearch.index.analysis.AbstractIndexAnalyzerProvider</code>的类；如果是Tokenizer还需要一个继承自<code>org.elasticsearch.index.analysis.AbstractTokenizerFactory</code>的类。</p>
<h2 id="3-实现abstracttokenizerfactory">3. 实现AbstractTokenizerFactory</h2>
<p>先来看继承自<code>AbstractTokenizerFactory</code>的<code>Tokenizer</code>的工厂类:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">org.elasticsearch.demo</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">org.apache.lucene.analysis.Tokenizer</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.common.inject.Inject</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.common.inject.assistedinject.Assisted</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.common.settings.Settings</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.index.Index</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.index.analysis.AbstractTokenizerFactory</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.index.settings.IndexSettingsService</span><span class="o">;</span>


<span class="kd">public</span> <span class="kd">class</span> <span class="nc">DemoTokenizerFactory</span> <span class="kd">extends</span> <span class="n">AbstractTokenizerFactory</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="nf">DemoTokenizerFactory</span><span class="o">(</span><span class="n">Index</span> <span class="n">index</span><span class="o">,</span> <span class="n">Settings</span> <span class="n">indexSettings</span><span class="o">,</span> <span class="n">String</span> <span class="n">name</span><span class="o">,</span> <span class="n">Settings</span> <span class="n">settings</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">super</span><span class="o">(</span><span class="n">index</span><span class="o">,</span> <span class="n">indexSettings</span><span class="o">,</span> <span class="n">name</span><span class="o">,</span> <span class="n">settings</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="cm">/**
</span><span class="cm">     * 必须实现此方法，而且需要标记为@Inject
</span><span class="cm">     *
</span><span class="cm">     * @param index
</span><span class="cm">     * @param indexSettingsService
</span><span class="cm">     * @param name
</span><span class="cm">     * @param settings
</span><span class="cm">     */</span>
    <span class="nd">@Inject</span>
    <span class="kd">public</span> <span class="nf">DemoTokenizerFactory</span><span class="o">(</span>
            <span class="n">Index</span> <span class="n">index</span><span class="o">,</span> <span class="n">IndexSettingsService</span> <span class="n">indexSettingsService</span><span class="o">,</span>
            <span class="nd">@Assisted</span> <span class="n">String</span> <span class="n">name</span><span class="o">,</span> <span class="nd">@Assisted</span> <span class="n">Settings</span> <span class="n">settings</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">super</span><span class="o">(</span><span class="n">index</span><span class="o">,</span> <span class="n">indexSettingsService</span><span class="o">.</span><span class="na">getSettings</span><span class="o">(),</span> <span class="n">name</span><span class="o">,</span> <span class="n">settings</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="n">Tokenizer</span> <span class="nf">create</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="k">new</span> <span class="n">DemoTokenizer</span><span class="o">();</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p>在2.3.4的es版本里面使用的是Guice依赖注入框架，所以必须实现上面标注了@Inject的构造方法。还要实现<code>public Tokenizer create()</code>方法，然后在create方法里面new自己的Tokenizer即可。</p>
<h2 id="4-实现abstractindexanalyzerprovider">4. 实现AbstractIndexAnalyzerProvider</h2>
<p>再来看看Analyzer的Provider：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">org.elasticsearch.demo</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">org.apache.lucene.analysis.Analyzer</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.common.inject.Inject</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.common.inject.assistedinject.Assisted</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.common.settings.Settings</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.env.Environment</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.index.Index</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.index.analysis.AbstractIndexAnalyzerProvider</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.elasticsearch.index.settings.IndexSettingsService</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">DemoAnalyzerProvider</span> <span class="kd">extends</span> <span class="n">AbstractIndexAnalyzerProvider</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="nf">DemoAnalyzerProvider</span><span class="o">(</span><span class="n">Index</span> <span class="n">index</span><span class="o">,</span> <span class="n">Settings</span> <span class="n">indexSettings</span><span class="o">,</span> <span class="n">String</span> <span class="n">name</span><span class="o">,</span> <span class="n">Settings</span> <span class="n">settings</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">super</span><span class="o">(</span><span class="n">index</span><span class="o">,</span> <span class="n">indexSettings</span><span class="o">,</span> <span class="n">name</span><span class="o">,</span> <span class="n">settings</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="cm">/**
</span><span class="cm">     * 必须实现此方法，而且需要标记为@Inject
</span><span class="cm">     *
</span><span class="cm">     * @param index
</span><span class="cm">     * @param indexSettingsService
</span><span class="cm">     * @param env
</span><span class="cm">     * @param name
</span><span class="cm">     * @param settings
</span><span class="cm">     */</span>
    <span class="nd">@Inject</span>
    <span class="kd">public</span> <span class="nf">DemoAnalyzerProvider</span><span class="o">(</span><span class="n">Index</span> <span class="n">index</span><span class="o">,</span> <span class="n">IndexSettingsService</span> <span class="n">indexSettingsService</span><span class="o">,</span> <span class="n">Environment</span> <span class="n">env</span><span class="o">,</span>
                                <span class="nd">@Assisted</span> <span class="n">String</span> <span class="n">name</span><span class="o">,</span> <span class="nd">@Assisted</span> <span class="n">Settings</span> <span class="n">settings</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">super</span><span class="o">(</span><span class="n">index</span><span class="o">,</span> <span class="n">indexSettingsService</span><span class="o">.</span><span class="na">getSettings</span><span class="o">(),</span> <span class="n">name</span><span class="o">,</span> <span class="n">settings</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="n">Analyzer</span> <span class="nf">get</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="k">new</span> <span class="n">DemoAnalyzer</span><span class="o">();</span>
    <span class="o">}</span>

<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p>跟上面的Tokenizer一样，必须实现上面代码标注了@Inject的构造方法，然后就需要实现<code>public Analyzer get()</code>方法，在这个get方法里面可以new自己的Analyzer</p>
<h2 id="5-package">5. package</h2>
<p>这需要使用到maven的assembly插件来对代码以及相应的资源打包，这样就可以使用es提供的plugin命令行工具进行安装了。</p>
<p>首先需要在<code>pom.xml</code>添加assembly插件：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;plugin&gt;</span>
	<span class="nt">&lt;artifactId&gt;</span>maven-assembly-plugin<span class="nt">&lt;/artifactId&gt;</span>
	<span class="nt">&lt;configuration&gt;</span>
		<span class="nt">&lt;outputDirectory&gt;</span>${project.build.directory}/releases/<span class="nt">&lt;/outputDirectory&gt;</span>
		<span class="nt">&lt;descriptors&gt;</span>
			<span class="nt">&lt;descriptor&gt;</span>${basedir}/src/main/assembly/plugin.xml<span class="nt">&lt;/descriptor&gt;</span>
		<span class="nt">&lt;/descriptors&gt;</span>
	<span class="nt">&lt;/configuration&gt;</span>
	<span class="nt">&lt;executions&gt;</span>
		<span class="nt">&lt;execution&gt;</span>
			<span class="nt">&lt;phase&gt;</span>package<span class="nt">&lt;/phase&gt;</span>
			<span class="nt">&lt;goals&gt;</span>
				<span class="nt">&lt;goal&gt;</span>single<span class="nt">&lt;/goal&gt;</span>
			<span class="nt">&lt;/goals&gt;</span>
		<span class="nt">&lt;/execution&gt;</span>
	<span class="nt">&lt;/executions&gt;</span>
<span class="nt">&lt;/plugin&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>然后添加<code>src/main/assembly/plugin.xml</code>，这个文件也可以放到其他地方，主要是上面的<code>descriptor</code>节点指定</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-xml" data-lang="xml"><span class="cp">&lt;?xml version=&#34;1.0&#34;?&gt;</span>
<span class="nt">&lt;assembly</span> <span class="na">xmlns=</span><span class="s">&#34;http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0&#34;</span> <span class="na">xmlns:xsi=</span><span class="s">&#34;http://www.w3.org/2001/XMLSchema-instance&#34;</span> <span class="na">xsi:schemaLocation=</span><span class="s">&#34;http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd&#34;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;id&gt;</span>release<span class="nt">&lt;/id&gt;</span>
    <span class="nt">&lt;formats&gt;</span>
        <span class="nt">&lt;format&gt;</span>jar<span class="nt">&lt;/format&gt;</span>
    <span class="nt">&lt;/formats&gt;</span>
    <span class="nt">&lt;includeBaseDirectory&gt;</span>false<span class="nt">&lt;/includeBaseDirectory&gt;</span>
    <span class="nt">&lt;fileSets&gt;</span>
        <span class="nt">&lt;fileSet&gt;</span>
            <span class="nt">&lt;directory&gt;</span>${project.basedir}/src/main/plugin-metadata<span class="nt">&lt;/directory&gt;</span>
            <span class="nt">&lt;outputDirectory&gt;</span>/<span class="nt">&lt;/outputDirectory&gt;</span>
        <span class="nt">&lt;/fileSet&gt;</span>
        <span class="nt">&lt;fileSet&gt;</span>
            <span class="nt">&lt;directory&gt;</span>${project.basedir}/lib<span class="nt">&lt;/directory&gt;</span>
            <span class="nt">&lt;outputDirectory&gt;</span>/<span class="nt">&lt;/outputDirectory&gt;</span>
        <span class="nt">&lt;/fileSet&gt;</span>
    <span class="nt">&lt;/fileSets&gt;</span>
    <span class="nt">&lt;files&gt;</span>
        <span class="nt">&lt;file&gt;</span>
            <span class="nt">&lt;source&gt;</span>${project.basedir}/src/main/resources/plugin-descriptor.properties<span class="nt">&lt;/source&gt;</span>
            <span class="nt">&lt;filtered&gt;</span>true<span class="nt">&lt;/filtered&gt;</span>
            <span class="nt">&lt;outputDirectory&gt;</span>/<span class="nt">&lt;/outputDirectory&gt;</span>
        <span class="nt">&lt;/file&gt;</span>
    <span class="nt">&lt;/files&gt;</span>
    <span class="nt">&lt;dependencySets&gt;</span>
        <span class="nt">&lt;dependencySet&gt;</span>
            <span class="nt">&lt;outputDirectory&gt;</span>/<span class="nt">&lt;/outputDirectory&gt;</span>
            <span class="nt">&lt;useProjectArtifact&gt;</span>true<span class="nt">&lt;/useProjectArtifact&gt;</span>
            <span class="nt">&lt;excludes&gt;</span>
                <span class="nt">&lt;exclude&gt;</span>org.elasticsearch:elasticsearch<span class="nt">&lt;/exclude&gt;</span>
                <span class="nt">&lt;exclude&gt;</span>org.apache.lucene:lucene*<span class="nt">&lt;/exclude&gt;</span>
                <span class="nt">&lt;exclude&gt;</span>com.spatial4j:spatial4j<span class="nt">&lt;/exclude&gt;</span>
            <span class="nt">&lt;/excludes&gt;</span>
        <span class="nt">&lt;/dependencySet&gt;</span>
    <span class="nt">&lt;/dependencySets&gt;</span>
<span class="nt">&lt;/assembly&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>根据自己需要做相应修改。基本上就是上面一些东西，这些坑都填平了，其他就就可以进行打包安装了</p>
<h2 id="6-打包安装">6. 打包安装</h2>
<p>在source根目录下使用<code>mvn clean install</code>。然后到ES根目录使用下面的命令进行插件的安装:</p>
<pre><code>./bin/plugin install file:///absolute/path/to/source/target/release/xxxx.jar
</code></pre>
<p>Windows使用：</p>
<pre><code>.\bin\plugin.bat install file:///C:/absolute/path/to/source/target/release/xxxx.jar
`
</code></pre>
<p>好了。</p>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">Kevin Jiang</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2016-12-16
        
    </span>
  </p>
  
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content"><a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank">CC BY-NC-ND 4.0</a></span>
  </p>
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/wechat_pay_1242x1242.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/alipay_600x600.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/elasticsearch/">Elasticsearch</a>
          <a href="/tags/%E5%88%86%E8%AF%8D/">分词</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/%E6%88%91%E7%9A%84%E5%BF%85%E5%A4%87%E5%B7%A5%E5%85%B7%E7%AE%B1/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">我的必备工具箱</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/emacs/spacemacs%E9%85%8D%E7%BD%AEfigwheel%E7%8E%AF%E5%A2%83/">
            <span class="next-text nav-default">spacemacs配置figwheel环境</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wenlin1988@126.com" class="iconfont icon-email" title="email"></a>
      <a href="https://www.linkedin.com/in/%E6%96%87%E6%9E%97-%E8%92%8B-0a3204126/" class="iconfont icon-linkedin" title="linkedin"></a>
      <a href="https://github.com/kevindragon" class="iconfont icon-github" title="github"></a>
  <a href="http://kevinjiang.info/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 -
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy;
    2015 -
    2023<span class="heart"><i class="iconfont icon-heart"></i></span><span>Kevin Jiang</span>
    <a href="https://beian.miit.gov.cn/" target="_blank">湘ICP备2022022745号</a>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        tags: 'ams',
        }
    };
  </script>
  <script type="text/javascript" async src="/lib/mathjax/es5/tex-mml-chtml.js"></script>

<script id="baidu_analytics">
  var _hmt = _hmt || [];
  (function() {
    if (window.location.hostname === 'localhost') return;
    var hm = document.createElement("script"); hm.async = true;
    hm.src = "https://hm.baidu.com/hm.js?b73ff6d4afc4af9e582d8a5dc068bab9";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>






</body>
</html>
